package com.lin.filegraph.utils.filedg;

import java.util.*;

/**
 * @description:
 * @author: linhuaixu
 * @time: 2023/5/18 10:30
 */

public class CouplingInfo {
    private final Map<CouplingType, Integer> mapOfCouplingCounter;
    private int numerOfElements;
    private int numberOfCouplingElements;

    public CouplingInfo() {
        mapOfCouplingCounter = new HashMap<>();
//		this.numerOfElements = numerOfElements;
//		this.numberOfCouplingElements = numberOfCouplingElements;
    }

    /*
    public CouplingInfo(Map<CouplingType, Integer> mapOfCouplingCounter) {
        this.mapOfCouplingCounter = mapOfCouplingCounter;
    }
     */
    public CouplingInfo(Map<CouplingType, Integer> mapOfCouplingCounter) {
        this.mapOfCouplingCounter = deepCopy(mapOfCouplingCounter);
    }

    public static Map<CouplingType, Integer> deepCopy(Map<CouplingType, Integer> original){
        Map<CouplingType, Integer> copy = new HashMap<CouplingType, Integer>();
        for(Map.Entry<CouplingType, Integer> entry : original.entrySet()){
            copy.put(entry.getKey(), entry.getValue());
        }
        return copy;
    }

    public void addCouplingCount(CouplingType type) {
        Integer count = mapOfCouplingCounter.get(type);
        if (count == null) {
            mapOfCouplingCounter.put(type, 1);
        } else {
            mapOfCouplingCounter.put(type, count + 1);
        }
    }

    public void removeCouplingCount(List<CouplingType> types) {
        for (CouplingType type : types) {
            mapOfCouplingCounter.remove(type);
        }
    }

    public void addCouplingCount(CouplingType type, int number) {
        Integer count = mapOfCouplingCounter.get(type);
        if (count == null) {
            mapOfCouplingCounter.put(type, number);
        } else {
            mapOfCouplingCounter.put(type, count + number);
        }
    }

    public Map<CouplingType, Integer> getMapOfCouplingCounter() {
        return Collections.unmodifiableMap(mapOfCouplingCounter);
    }

    public int getCouplingCountByType(CouplingType type) {
        Integer count = mapOfCouplingCounter.get(type);
        if (count == null) {
            return 0;
        } else {
            return count;
        }
    }

    public int getCombinationCount() {
        return getCouplingCountByType(CouplingType.Combination);
    }

    public int getOthersCount() {
        int count = 0;
        for (CouplingType ct : CouplingType.values()) {
            if (ct != CouplingType.Combination && ct != CouplingType.Implements && ct != CouplingType.Inheritance && ct != CouplingType.DeclareToDefine) {
                count += getCouplingCountByType(ct);
            }
        }
        return count;
    }

    public Set<CouplingType> getCouplingTypes() {
        return mapOfCouplingCounter.keySet();
    }

    public boolean hasCoupling() {
        int count = 0;
        if (mapOfCouplingCounter.values().size() > 0) {
            return true;
        } else {
            return false;
        }
    }

    public int getNumerOfElements() {
        return numerOfElements;
    }

    public void setNumerOfElements(int numerOfElements) {
        this.numerOfElements = numerOfElements;
    }

    public int getNumberOfCouplingElements() {
        return numberOfCouplingElements;
    }

    public void setNumberOfCouplingElements(int numberOfCouplingElements) {
        this.numberOfCouplingElements = numberOfCouplingElements;
    }
}
